/**   
 * @Title: BindMobile_unBind.java 
 * @Package： com.esun.rabbit2.viewer.mainact.mylotteryact.bindmobile 
 * @Description: 未绑定过手机号
 * @Author Miko baoz@500wan.com   
 * @Date 2011-7-3 下午06:25:15  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.mylotteryact.bindmobile;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.esun.config.Config;
import com.esun.config.Language;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.communicaters.myinfo.BindMobileCommunicater;
import com.esun.ui.R;
import com.esun.util.EditTextInputUitl;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

/**
 * @ClassName: BindMobile_unBind
 * @Description:未绑定过手机号Activity
 * @author Miko baoz@500wan.com
 * @date 2011-7-3 下午06:25:15
 * 
 */
public class BindMobile_unBind extends Activity implements OnClickListener {

	private ImageButton btnGoBack;
	private Button btnSubmit;

	private EditText etNum = null;
	private Button btnNumClear;

	private String strNum;

	private Handler handler;

	private static final int BIND_SUCCESS = 10001;
	private static final int BIND_FAILED = 10002;

	private ProgressDialog progress;

	BindMobileCommunicater communicater = new BindMobileCommunicater();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bind_mobile_unbind);
		init();
		initHandler();
	}

	private void init() {
		btnSubmit = (Button) this.findViewById(R.id.bind_mobile_unbind_btn_submit);
		btnSubmit.setOnClickListener(this);

		btnGoBack = (ImageButton) this.findViewById(R.id.bind_mobile_unbind_btn_goBack);
		btnGoBack.setOnClickListener(this);

		etNum = (EditText) this.findViewById(R.id.bind_mobile_unbind_et_num);
		etNum.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				EDebug.o(this, s.toString());
				if (s.length() == 11) {
					Toast.makeText(BindMobile_unBind.this, Language.STR_INPUT_RIGHT_PHONENUM, Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		btnNumClear = (Button) this.findViewById(R.id.bind_mobile_unbind_et_num_btn_claen);
		etNum = new EditTextInputUitl(etNum, btnNumClear).getEditText();

		progress = ViewerUtil.showProgressDialog(this, "请稍后...");
	}

	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				progress.dismiss();

				switch (msg.what) {
				case BIND_SUCCESS:

					ViewerUtil.showTipDialog(BindMobile_unBind.this, communicater.getServerMsg(), true);

					break;

				case BIND_FAILED:
					ViewerUtil.showTipDialog(BindMobile_unBind.this, communicater.getServerMsg(), false);
					break;

				case Config.CONNECTION_FAIL:
				  Toast.makeText(BindMobile_unBind.this, Language.STR_CONNECTIONFAIL.toString(), 0).show();
//					ViewerUtil.showTipDialog(BindMobile_unBind.this, Language.BINDING_MOBEL_FAIL, false);

					break;

				default:
					break;
				}
			}
		};
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.bind_mobile_unbind_btn_submit:
			if (checkInput()) {
				progress.show();
				startThread();
			}
			break;

		case R.id.bind_mobile_unbind_btn_goBack:
			this.finish();
			break;

		default:
			break;
		}
	}

	private boolean checkInput() {

		strNum = etNum.getText().toString();

		if (strNum.length() < 1) {
			ViewerUtil.showTipDialog(this, "请输入手机号", false);
			return false;
		} else {
			return true;
		}
	}

	private void startThread() {
		new Thread() {

			@Override
			public void run() {
				super.run();
				doSubmit();
			}
		}.start();
	}

	private void doSubmit() {

		try {
			if (communicater.bind(strNum)) {
				Message msg = handler.obtainMessage(BIND_SUCCESS);
				handler.sendMessage(msg);
			} else {
				Message msg = handler.obtainMessage(BIND_FAILED);
				handler.sendMessage(msg);
			}
		} catch (Exception e) {
			Message msg = handler.obtainMessage(Config.CONNECTION_FAIL);
			handler.sendMessage(msg);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
	}

}
